local skel = fk.CreateSkill {
  name = "lb__shixian",
}

Fk:loadTranslationTable {
  ["lb__shixian"] = "拭弦",
  [":lb__shixian"] = "每回合限一次，你的体力值变化后可以令一名角色执行并移除一项：<br>" ..
      "1.摸两张牌，若仅剩该项则额外执行一次；<br>" ..
      "2.回复1点体力，若仅剩该项则额外执行一次；<br>" ..
      "3.移动场上一张牌，若仅剩该项则额外执行一次；<br>" ..
      "4.翻面，若仅剩该项，你翻面并重置其他所有项。",
    
  ["@lb__shixian"] = "拭弦",

  ["#lb__shixian-1"] = "摸两张牌",
  ["#lb__shixian-2"] = "回复1点体力",
  ["#lb__shixian-3"] = "移动场上一张牌",
  ["#lb__shixian-4"] = "翻面",

  ["lb__shixian_choose"] = "拭弦",
  ["#lb__shixian"] = "拭弦：你可以令一名角色执行一项",
  ["#lb__shixian-move"] = "拭弦：请选择两名角色，移动场上的一张牌",

  ["$lb__shixian1"] = "琴音，周而复始。",
  ["$lb__shixian2"] = "等待，亦是变化。",
  ["$lb__shixian3"] = "都会有被风吹落的…那一天…",
}

local changeMark = function(player)
  local lang = { "摸", "回", "移", "翻", }
  local mark = table.concat(table.map(player:getTableMark(skel.name), function(num)
    return lang[num]
  end), "/")
  player.room:setPlayerMark(player, "@lb__shixian", mark)
end

skel:addEffect(fk.HpChanged, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player == target and
        player:usedSkillTimes(skel.name, Player.HistoryTurn) == 0 and
        #player:getTableMark("lb__shixian") > 0
        and data.num ~= 0 -- 淦，为什么护甲也会触发这个
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all, ask = {}, {}
    for i = 1, 4, 1 do
      local choice = "#lb__shixian-"..i
      table.insert(all, choice)
      if not (i == 3 and #room:canMoveCardInBoard("ej") == 0) and table.contains(player:getTableMark("lb__shixian"), i) then
        table.insert(ask, choice)
      end
    end
    if #ask == 0 then return end
    local s, ret = room:askToUseActiveSkill(player, {
      skill_name = "#lb__shixian_choose",
      prompt = "#lb__shixian",
      extra_data = {
        skillName = skel.name,
        extra_data = {
          list = ask,
        }
      }
    })
    if s and ret and ret.targets and ret.interaction then
      event:setCostData(self, { tos = ret.targets, num = table.indexOf(all, ret.interaction) })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local t = event:getCostData(self).tos[1]
    local i = event:getCostData(self).num
    room:removeTableMark(player, "lb__shixian", i)
    local n = #player:getTableMark("lb__shixian") == 0 and 2 or 1
    for j = 1, n, 1 do
      if i == 1 and t:isAlive() then
        t:drawCards(2, skel.name)
      elseif i == 2 and t:isAlive() then
        if t:isWounded() then
          room:recover {
            who = t,
            num = 1,
            skillName = skel.name,
            recoverBy = player,
          }
        end
      elseif i == 3 and t:isAlive() then
        local targets = room:askToChooseToMoveCardInBoard(t, {
          prompt = "#lb__shixian-move",
          skill_name = skel.name,
          cancelable = false,
        })
        if #targets == 2 then
          room:askToMoveCardInBoard(t, {
            target_one = targets[1],
            target_two = targets[2],
            skill_name = skel.name,
          })
        end
      elseif i == 4 then
        if j == 1 then
          if t:isAlive() then
            t:turnOver()
          end
        else
          if player:isAlive() then
            player:turnOver()
            room:setPlayerMark(player, "lb__shixian", { 1, 2, 3 })
          end
        end
      end
    end
    changeMark(player)
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  player.room:setPlayerMark(player, "lb__shixian", {1, 2, 3, 4})
  changeMark(player)
end)

skel:addLoseEffect(function(self, player, is_death)
  player.room:setPlayerMark(player, "lb__shixian", 0)
  player.room:setPlayerMark(player, "@lb__shixian", 0)
end)

return skel
